package app.utils;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import app.utils.BaiduMapPOISearchBean;

/**
 * 这个类用于调用百度的web服务API
 * @author Haoqi
 *2017-11-29 22:32:00
 */
public class BaiduMapUtils {
	
	private String  webKey ="986LavcZs69Zk8jQbCrr02SZTSrfmKEe";
	
	/**
	 * 以传入的经纬度为圆心，搜索半径radius内的与address有关的信息，返回地址和经纬度
	 * @param address 关键词
	 * @param lng 经度
	 * @param lat 纬度
	 * @param radius 搜索半径
	 * @return
	 * @throws Exception
	 */
    public List<BaiduMapAddressBean> POISearch(String address,String lng,String lat,int radius) throws Exception{
    	
    	
		URL url = new URL("https://api.map.baidu.com/place/v2/search?query="+address+"&location="+lat+","+lng+"&radius="+radius+"&radius_limit=true&output=json&coord_type=1&ak="+webKey);
		
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.connect();
		
		InputStream inputStream = connection.getInputStream();
		
		//字符缓冲区
		StringBuilder builder=new StringBuilder();
		
		byte[] buf = new byte[1024];
		
		int len =0;
		while((len=inputStream.read(buf))>0){
			
			builder.append(new String(buf,0,len,"UTF-8"));

			
		}
		//System.out.println(builder.toString());
		inputStream.close();
		
		
		Gson gson = new Gson();
		BaiduMapPOISearchBean baiduMapPOISearchBean = gson.fromJson(builder.toString(), BaiduMapPOISearchBean.class);
		
		Results[] results = baiduMapPOISearchBean.getResults();
		
		if (results.length<=0) {
			
			return null;
		}
		
		List<BaiduMapAddressBean> list = new ArrayList<>();
		
		for (Results results2 : results) {
		
			BaiduMapAddressBean bmab = new BaiduMapAddressBean();
			
			bmab.setAddress(results2.getName());
			
			Location location = results2.getLocation();
			
			bmab.setLng(location.getLng());
			
			bmab.setLat(location.getLat());
			
			list.add(bmab);
			
		}
		
		return list;
		
		
    }
    
    /**
     * 将wgs84坐标系坐标转换为百度bd09坐标，返回  经度,纬度
     * @param lng
     * @param lat
     * @return
     * @throws Exception
     */
    public String wgs84Tobd09(String lng,String lat) throws Exception{
    	
		URL url = new URL("https://api.map.baidu.com/geoconv/v1/?coords="+lng+","+lat+"&from=1&to=5&ak="+webKey);
		HttpURLConnection huc = (HttpURLConnection) url.openConnection();
		huc.setDoOutput(true);
		huc.setDoInput(true);
		huc.setUseCaches(false);
		huc.connect();
		InputStream bais =huc.getInputStream();
		
		byte[]  tempByte = new byte[5];
		StringBuilder tempStr =new StringBuilder();
		if (bais!=null) {
			int read=0;
			while((read=bais.read(tempByte))>0){
				tempStr.append(new String(tempByte, 0, read));
			}
		}
		
		String splitJson = tempStr.toString();
		
		int startIndex = splitJson.indexOf("[{\"x\":");
		int midIndex = splitJson.indexOf(",\"y\":");
		int endIndex = splitJson.indexOf("}]}");

		String longitude = splitJson.substring(startIndex+6, midIndex);
		String latitude = splitJson.substring(midIndex+5, endIndex);

		
		String location = longitude+","+latitude;
		
		System.out.println(tempStr);

    	return location;
    }
	
	

}
